const express =require("express");
const app =express();
const mongoose=require("mongoose");
const bodyparser=require("body-parser")
const passport =require("passport");
const users=require("./routes/api/users");
const profiles=require("./routes/api/profiles");

//db config
const db=require("./config/key").mongoURL;


//使用bodyparser处理表单参数
app.use(bodyparser.urlencoded({extended:false}))
app.use(bodyparser.json());

//配置passport
app.use(passport.initialize());
require("./config/passport")(passport);//将passport传到passport.js文件中去
// connect db

mongoose.connect(db,{useNewUrlParser: true,useUnifiedTopology: true})
        .then(()=>console.log("MongoDB Connnected"))
        .catch(err=>console.log(err));


//使用routes
app.use("/api/users",users);
app.use("/api/profiles",profiles);

const port =process.env.PORT || 5000;
app.listen(port,()=>{
    console.log(`Server runing on port ${port}`);
});